package com.hsd.znsh.service.impl;

import com.hsd.znsh.converter.ProductConverter;
import com.hsd.znsh.entity.Product;
import com.hsd.znsh.entity.User;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.exception.ZnshException;
import com.hsd.znsh.pojo.output.ProductVO;
import com.hsd.znsh.pojo.query.ProductQuery;
import com.hsd.znsh.properties.SecurityProperties;
import com.hsd.znsh.repository.ProductRepository;
import com.hsd.znsh.repository.ProductTypeRepository;
import com.hsd.znsh.repository.UserRepository;
import com.hsd.znsh.service.ProductService;
import com.hsd.znsh.utils.BeanUtil;
import com.hsd.znsh.utils.FileUtil;
import com.hsd.znsh.utils.HqlUtil;
import com.hsd.znsh.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-26 11:30:33
 */
@Service
public class ProductSeviceImpl extends BaseServiceImpl implements ProductService {

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ProductTypeRepository productTypeRepository;

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    public long getProductListTotalPages(ProductQuery productQuery,Integer userId) {
        if(userId!=null) productQuery.setCreateUserId(userId);
        return this.count(HqlUtil.getCountHql(productQuery),productQuery);
    }

    @Override
    public List<ProductVO> getProductList(Pageable pageable, ProductQuery productQuery,Integer userId) {
        if(userId!=null) productQuery.setCreateUserId(userId);
        List<Product> list=this.find(HqlUtil.getQueryHql(productQuery),productQuery,pageable);
        return ProductConverter.converterToProductVOList(list,productTypeRepository,userRepository);
    }


    @Override
    public ProductVO getProductInfo(Integer id) {
        Product product=productRepository.findOne(id);
        return ProductConverter.converterToProductVO(product,productTypeRepository,userRepository);
    }

    @Override
    public Product addProduct(Product product, MultipartFile multipartFile) {
        if(multipartFile==null) throw new ZnshException("图片不能为空");
        product.setCreateUserId(SecurityUtil.getUserId());
        String filePath=securityProperties.getBrowser().getFilePath();
        String pictureAddress=FileUtil.copyToFile(multipartFile,filePath+"/product");
        product.setPictureAddress(pictureAddress);
        productRepository.save(product);
        return product;
    }

    @Override
    public Product updateProduct(Product product,MultipartFile multipartFile) {
        if(multipartFile!=null){
            String filePath=securityProperties.getBrowser().getFilePath();
            FileUtil.delFile(product.getPictureAddress());
            String pictureAddress=FileUtil.copyToFile(multipartFile,filePath+"/product");
            product.setPictureAddress(pictureAddress);
        }
        Product target=productRepository.findOne(product.getId());
        BeanUtil.copyPropertiesIgnoreNull(product,target);
        return productRepository.save(target);
    }

    @Override
    public void deleteProduct(Integer id) {
        Product product=productRepository.findOne(id);
        FileUtil.delFile(product.getPictureAddress());
        productRepository.delete(id);
    }

    @Override
    public List<Product> getProductListByAPPUser(User user) {
        return productRepository.findByCreateUserId(user.getCreateUserId());
    }
}
